// FudxSink.cpp: implementation of the CFudxSink class.
//
//////////////////////////////////////////////////////////////////////

#include "StdAfx.h"
#include "FudxSink.h"
#include "SendThread.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CFudxSink::CFudxSink()
{
	m_pTimer = CreateUdxTime();
	m_pFileTcp = NULL;

}

CFudxSink::~CFudxSink()
{
	if(m_pFileTcp)
	{
		m_pFileTcp->ReleaseLife();
		m_pFileTcp = NULL;

	}

	m_pTimer->Destroy();
}


void CFudxSink::OnStreamMsgRead( IUdxTcp * pTcp,BYTE* pData,int len )
{
	stMsg *pMsg = (stMsg*)pData;
	if(pMsg->type == 1)
	{
		pTcp->GetUdxCfg()->expectbew = pMsg->expectbew;
		pTcp->GetUdxCfg()->maxlostrate = pMsg->lost;
		new CSendThread(pTcp);
	}
	if(pMsg->type == 4) 
	{
		if(m_pFileTcp)
		{
			m_pFileTcp->ReleaseLife();
			m_pFileTcp = NULL;
			
		}
		UdxMsgEx * pFile = (UdxMsgEx*)pData;
		
		pTcp->GetUdxCfg()->expectbew = pFile->expectbew;
		pTcp->GetUdxCfg()->maxlostrate = pFile->lost;
		pTcp->SendFile(pFile->strcmd);

		pTcp->AddLife();
		m_pFileTcp = pTcp;
	}
}
void CFudxSink::OnStreamBroken( IUdxTcp * pTcp )
{
	CSendThread* pthread = (CSendThread*)pTcp->GetUserData();

	if(!pthread)
		return;

	delete pthread;
	
}

void CFudxSink::OnStreamRead( IUdxTcp * pTcp,BYTE* pData,int len )
{
// 	BYTE d[40];
// 	if(!pTcp->SendBuff(d,40))
// 	{
// 		printf("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&\n");
// 	}
}
extern void UpdateSpeed( IUdxTcp * pSocket);

void CFudxSink::OnFastUdxTimer()
{
	if(m_pFileTcp)
	{
		if(!m_pFileTcp->IsConnected() || m_pFileTcp->IsDone(TRUE) )
		{
			m_pFileTcp->ReleaseLife();
			m_pFileTcp = NULL;
		}else
		{
			UpdateSpeed(m_pFileTcp);
		}
	}

}